package com.lm.paymentadmin.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSONObject;
import com.lm.paymentadmin.common.Result;
import com.lm.paymentadmin.common.bean.PageEntity;
import com.lm.paymentadmin.domain.Book;
import com.lm.paymentadmin.domain.ToEmail;
import com.lm.paymentadmin.kafka.KafkaProducer;
import com.lm.paymentadmin.redis.RedisLock;
import com.lm.paymentadmin.service.*;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liu2dog
 */
@Slf4j
@RestController
@RequestMapping("/test")
@ApiIgnore
public class TestController {


    @Value("${jwt.secret}")
    private String secret;

    @Resource
    private RedisLock redisLock;

    @Resource
    private EmailService emailService;

    @Resource
    private EsService esService;

    @Resource
    private ExcelService excelService;






    @Resource
    private KafkaProducer kafkaProducer;

    public static final String TOPIC_TEST_FLINK = "test_sql";

    @SentinelResource(value = "test1")
    @GetMapping("/createBookIndex")
    @ResponseBody
    public Result createBookIndex()  throws BlockException{
        try {
            esService.createContentIk(Book.class);
        }catch (IOException e){
            e.printStackTrace();
        }
        return Result.ok("");
    }



    @PostConstruct
    public void initSentinelRule()
    {
        log.info("加载规则");
        List<FlowRule> flowRules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        //定义以QPS类型
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setResource("test1");
        rule.setCount(1);
        flowRules.add(rule);
        FlowRuleManager.loadRules(flowRules);
    }



    @GetMapping("/searchBook")
    public Result searchBook(@RequestParam(name = "page", required = false)Integer page,
                                @RequestParam(name = "size", required = false)Integer size){
        PageEntity pageEntity = new PageEntity();
        pageEntity.setPage(page);
        pageEntity.setPageSize(size);
        return Result.ok(esService.search(pageEntity,new SearchSourceBuilder(),Book.class));
    }



    @GetMapping("/insertBook")
    public Result insertBook(){
        List<Object> list = new ArrayList<>();
        Book book = new Book();
        book.setBookName("活着");
        book.setAuthor("余华");
        book.setBookType("当代文学");
        book.setId(1L);
        book.setPublishTime("1993-03-12");
        book.setDescription("伟大这个词在这本书面前太渺小");
        Book book1 = new Book();
        book1.setBookName("许三观卖血记");
        book1.setAuthor("余华");
        book1.setBookType("当代文学");
        book1.setId(2L);
        book1.setPublishTime("1998-03-12");
        book1.setDescription("《许三观卖血记》是余华于1995年创作的一部长篇小说，小说讲述了许三观靠着卖血渡过了人生的一个个难关，战胜了命运强加给他的惊涛骇浪，而当他老了，知道自己的血再也没有人要时，精神却崩溃了。它以博大的温情描绘了磨难中的人生，以激烈的故事形式表达了人在面对厄运时求生的欲望");
        Book book2 = new Book();
        book2.setBookName("百年孤独");
        book2.setAuthor("马尔克斯");
        book2.setBookType("长篇小说");
        book2.setId(3L);
        book2.setPublishTime("1988-03-12");
        book2.setDescription("《百年孤独》，是哥伦比亚作家加西亚·马尔克斯创作的长篇小说，是其代表作，也是拉丁美洲魔幻现实主义文学的代表作，被誉为“再现拉丁美洲历史社会图景的鸿篇巨著”。\n" +
                "作品描写了布恩迪亚家族七代人的传奇故事，以及加勒比海沿岸小镇马孔多的百年兴衰，反映了拉丁美洲一个世纪以来风云变幻的历史。作品融入神话传说、民间故事、宗教典故等神秘因素，巧妙地糅合了现实与虚幻，展现出一个瑰丽的想象世界，成为20世纪重要的经典文学巨著之一。");
        list.add(book);
        list.add(book1);
        list.add(book2);
        esService.batchAdd(list);
        return Result.ok("");
    }





    @PostMapping("/sendEmail")
    @ResponseBody
    public Result sendEmail(@RequestParam(name = "email", required = false)String email){
        ToEmail toEmail = new ToEmail();
        toEmail.setTos(new String[]{email});
        toEmail.setSubject("测试");
        toEmail.setContent("--------");
        log.info("-----------");
        String msg = emailService.sendEmail(toEmail);
        return Result.ok(msg);
    }


    @GetMapping("/test")
    @ResponseBody
    public Result test(){
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject o = new JSONObject();
        o.put("user_id",102);
        o.put("behavior","buy");
        o.put("ts",sbf.format(d));
        kafkaProducer.sendMessage(TOPIC_TEST_FLINK,o);
        return Result.ok("成功");
    }

    @GetMapping("/testExcel")
    @ResponseBody
    public Result testExcel(){
        excelService.readExcel();
        return Result.ok("成功");
    }


}
